package master.photo.propertyeditor;

import java.beans.PropertyEditorSupport;

import master.photo.domain.security.UserRole;
import master.photo.service.UserRoleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class UserRolePropertyEditor extends PropertyEditorSupport {

	@Autowired
	private UserRoleService userRoleService;

	@Override
	public void setAsText(String text) throws IllegalArgumentException {
		long roleId = Long.parseLong(text);
		setValue(userRoleService.load(roleId));
	}

	@Override
	public String getAsText() {
		UserRole role = (UserRole) getValue();
		return role.getId().toString();
	}

}
